package auto.app.service.impl;

import auto.app.controller.from.AutoExtractionAddFrom;
import auto.app.controller.from.AutoExtractionAddListFrom;
import auto.app.exetion.MainExection;
import auto.app.model.AutoExtraction;
import auto.app.dao.AutoExtractionDao;
import auto.app.service.AutoExtractionService;
import auto.app.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;

import static auto.app.exetion.MainExetionMessage.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-07-22
 */
@Service
public class AutoExtractionServiceImpl extends ServiceImpl<AutoExtractionDao, AutoExtraction> implements AutoExtractionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addExtraction(AutoExtractionAddListFrom autoExtractionAddListFrom) throws MainExection {
        //判断列表为空
        List<AutoExtractionAddFrom> autoExtractionAddFromListFroms = autoExtractionAddListFrom.getAutoExtractionAddFromListFroms();
        if (autoExtractionAddFromListFroms.size() == 0 || CollectionUtils.isEmpty(autoExtractionAddFromListFroms)) {
            throw new MainExection(EXTRACTIONADDERROR.getCode(), EXTRACTIONADDERROR.getMsg());
        }
        //将数据入库
        List<AutoExtraction> autoExtractions = CommonUtil.copyList(autoExtractionAddFromListFroms, AutoExtraction.class);
        boolean b = this.saveBatch(autoExtractions);
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteExtraction(Integer id) throws MainExection {
        //查询关联提取存在不
        AutoExtraction autoExtraction = baseMapper.selectById(id);
        if (autoExtraction == null) {
            throw new MainExection(EXTRACTIONADDSELECTERROR.getCode(), EXTRACTIONADDSELECTERROR.getMsg());
        }
        //存在的情况下走删除
        boolean b = this.removeById(id);
        return b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteExtractionList(List<Integer> ids) throws MainExection {
        boolean b = removeByIds(ids);
        return b;
    }

    @Override
    public List<AutoExtraction> list(Integer caseId) throws MainExection {
        //查询caseid的关联提取数据
        QueryWrapper<AutoExtraction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("case_id", caseId);
        List<AutoExtraction> autoExtractions = baseMapper.selectList(queryWrapper);
//        if (CollectionUtils.isEmpty(autoExtractions)) {
//            throw new MainExection(EXTRACTIONADDSELECTLISTERROR.getCode(), EXTRACTIONADDSELECTLISTERROR.getMsg());
//        }
        return autoExtractions;
    }
}
